# 使用到了jq 库， 请提前安装 yum install jq

date_cur=$(date "+%Y%m%d")
date_cur='20231015'
#curl -s  https://gitee.com/dogsled/lddb_chandao/raw/master/dingding.md |grep $date_cur && echo ok || exit

homepath="/Users/shangshang/Documents/android-tools/platform-tools/"

#curl -s http://www.easybots.cn/api/holiday.php\?d\=20200115 |grep ":\"0"

echo 当前时间$(date "+%Y-%m-%d %H:%M:%S")

weekofday=$(date +%w)
cur_hour=$(date +%H)

#$homepath/adb shell input keyevent 26

# echo "判断当前日期是否是工作日或假期: 工作日为状态为：0"
date_cur=`date "+%Y%m%d"`
#date_cur=20231015
cur_stat=`curl -s  https://tool.bitefu.net/jiari/\?d\=$date_cur\&info\=1\&back\=json |grep "\"type\":0"|wc -l`
if [  $cur_stat -eq 0  ]; then
	echo "启用备用， 查询链接，防止主链接异常"
	sleep 3 # 防止被加黑名单
	cur_stat=`curl -s  http://tool.bjzrch.com/jiari/\?d\=$date_cur\&info\=1\&back\=json |grep "\"type\":0"|wc -l` # backup url
fi

echo 当前日期为：${date_cur},工作日状态为：$cur_stat
if [ $# -eq 1 ]; then
	cur_stat=$1
	echo "接收到参数：$cur_stat "
	num=$((RANDOM % 20)) #1分钟(60s)后执行
	echo "sleep $num 秒"
	weekofday=$1 #强制打卡
else
	echo 生成随机数
	#num=$((RANDOM%25*60))
	num=$((RANDOM % 60)) #10分钟(600s)后执行
	#num=$((RANDOM%300)) #40分钟(2400s)后执行
	echo "sleep $num"
	echo "sleep $((num / 60)) 分钟"

fi
sleep $num

echo 点亮屏幕

#cur_stat=1
if [[  $cur_stat -eq 0  ||  $weekofday == 6 ]];then
#if [[ $weekofday == 0 || $weekofday == 6 ]]; then
	echo "当前是节假日。无需打卡，如无加班【即将退出】"
	curl -s  https://gitee.com/dogsled/lddb_chandao/raw/master/dingding.md |grep $date_cur && echo "节假日加班，需要的打卡" || exit
	#exit
else
    echo "判断有没有官方请假"
    curl -s  https://gitee.com/dogsled/lddb_chandao/raw/master/dingding.md |grep "请假-$date_cur" && exit || "工作日需要打卡"
    echo "当前是工作日，继续打卡
fi

echo 当前时间$(date "+%Y-%m-%d %H:%M:%S")
source ~/.bashrc

echo 开始自动打卡$(date "+%Y-%m-%d %H:%M:%S")
echo "./adb tcpip 5556 开启wifi 调试"
echo 获取设备列表
$homepath/adb devices
if [ $? -eq 1 ]; then
	$homepath/adb connect 192.168.13.82:5556
fi
$homepath/adb connect 192.168.13.82:5556

echo "等待1秒解锁"
sleep 1
$homepath/adb shell dumpsys window policy | grep "screenState=SCREEN_STATE_ON"
if [ $? -eq 1 ]; then
	echo 屏幕未点亮，唤醒屏幕
	echo 唤醒屏幕
	$homepath/adb shell input keyevent 26
else
	echo 屏幕已点亮

fi

$homepath/adb shell dumpsys window policy | grep "isStatusBarKeyguard=false"
if [ $? -eq 1 ]; then
	echo 屏幕锁定，需要解锁
	$homepath/adb shell input keyevent 82
	echo "第一次滑动解锁，去除充电蒙层"
	$homepath/adb shell input swipe 300 1000 300 500
	sleep 1
	echo "第二次滑动解锁，真正去解锁"
	$homepath/adb shell input swipe 300 1000 300 500
	$homepath/adb shell input keyevent 3
	$homepath/adb shell input keyevent 4
else
	echo 屏幕已解锁

fi

echo 打开gps
$homepath/adb shell settings put secure location_providers_allowed +gps
echo 打开wifi
$homepath/adb shell svc wifi enable

echo 先杀进程
$homepath/adb shell am force-stop com.alibaba.android.rimet

echo 等待4秒
sleep 4

echo 打开钉钉
$homepath/adb shell am start -n com.alibaba.android.rimet/com.alibaba.android.rimet.biz.LaunchHomeActivity

echo 等待6秒
sleep 6
echo 点击工作台
#$homepath/adb shell input tap 360 1300
$homepath/adb shell input tap 555 2222
echo 等待10秒
sleep 10
echo 点击考勤打卡
#$homepath/adb shell input tap 100 880
$homepath/adb shell input tap 150 1000
echo 等待5秒
sleep 5
echo 点击打卡
#$homepath/adb shell input tap 380  780
$homepath/adb shell input tap 550 1400

echo 等待5秒
sleep 5
echo 杀掉进程
$homepath/adb shell am force-stop com.alibaba.android.rimet

echo 关闭屏幕
$homepath/adb shell input keyevent 26
echo 打卡完成
echo 当前时间:$(date "+%Y-%m-%d %H:%M:%S")
#curl "https://iyuu.cn/IYUU4483Tb9df029ed637573c59ee108dbaa142e1dcc459ac.send?text=ding_AutoPin_finished"
curl "https://iyuu.cn/IYUU4483Tb9df029ed637573c59ee108dbaa142e1dcc459ac.send?text=%E9%92%89%E9%92%89%E3%80%90%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0%E3%80%91%E5%AE%8C%E6%88%90"
echo "\n\n"